/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sys;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JList;
import javax.swing.ListModel;
import javax.swing.event.ListDataListener;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

import sources.BlipDay9Tv;
import sources.SourceInterface;

/**
 *
 * @author Jonas
 */
public class Videos implements ListModel {

    private List<SourceInterface> sources;
    private List<ListDataListener> listeners;
    private List<Video> videos;
    private ArrayList<String> sourceStrings;
    private ArrayList<String> gameStrings;
    private ArrayList<String> producerStrings;

    public Videos() {
        super();
        listeners = new ArrayList<ListDataListener>();
        videos = new ArrayList<Video>();
        sources = new ArrayList<SourceInterface>();
        
        sourceStrings = new ArrayList<String>();
        gameStrings = new ArrayList<String>();
        producerStrings = new ArrayList<String>();
        
        //TODO: Make modular
        try {
			sources.add(new BlipDay9Tv());
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		//End
		
        for(SourceInterface source : sources) {
        	for(Video v : source.getVideos()) {
        		if(!sourceStrings.contains(v.getSource())) {
        			sourceStrings.add(v.getSource());
        		}
				if(!gameStrings.contains(v.getGame())) {
				    gameStrings.add(v.getGame());    			
				}
				if(!producerStrings.contains(v.getProducer())) {
					producerStrings.add(v.getProducer());
				}
        		videos.add(v);
        	}
        }
        
    }

    public Object getElementAt(int index) {
        return videos.get(index);
    }

    public void addListDataListener(ListDataListener l) {
        listeners.add(l);
    }

    public void removeListDataListener(ListDataListener l) {
        listeners.remove(l);
    }

    public int getSize() {
        return videos.size();
    }

    public void removeVideo(Video video) {
    	this.videos.remove(video);
    }
    
    public List<String> getSourceStrings() {
    	return sourceStrings;
    }
    
    public List<String> getGameStrings() {
    	return gameStrings;
    }
    
    public List<String> getProducerStrings() {
    	return producerStrings;
    }
    
    public List<Video> getVideos() {
    	return videos;
    }
    
    public List<Video> getFilteredVideos(String game, String source, String producer) {
    	ArrayList<Video> filteredVideos = new ArrayList<Video>();
    	for(Video v : videos) {
    		if(v.getGame().equals(game) && v.getSource().equals(source) && v.getProducer().equals(producer)) {
    			filteredVideos.add(v);
    		}
    	}
    	return filteredVideos;
    }
    
}
